#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace OshHost
{
	/// <summary>
	/// 
	/// </summary>
	public class ExceptionForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel buttonsPanel;
		private System.Windows.Forms.Button okButton;

		private System.Windows.Forms.Button moreButton;
		private System.Windows.Forms.Button lessButton;
		private System.Windows.Forms.Label errorLabel;
		private System.Windows.Forms.TextBox detailsTextBox;
		private System.Windows.Forms.Panel detailsPanel;

		//
		private const int _briefFormHeight = 160;
		private const int _detailedFormHeight = 400;
		private Exception _exception;
		private Exception _shownException;

		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ExceptionForm()
		{
			InitializeComponent();
		}

		public ExceptionForm(Exception exception)
		{
			if(exception == null)
				throw new ArgumentNullException("exception");

			InitializeComponent();

			lessButton.Left = moreButton.Left;
			lessButton.Visible = false;
			this.FormBorderStyle = FormBorderStyle.FixedDialog;
			this.Height = _briefFormHeight;

			_exception = exception;
			_shownException = null;

			DisplayNextMessage();

		}

		private void DisplayNextMessage()
		{
			if(_shownException == null || _shownException.InnerException == null)
				_shownException = _exception;
			else
				_shownException = _shownException.InnerException;

			errorLabel.Text = _shownException.Message;
			detailsTextBox.Text = _shownException.ToString();
		}


		private void errorLabel_Click(object sender, System.EventArgs e)
		{
			DisplayNextMessage();
		}

		private void okButton_Click(object sender, System.EventArgs e)
		{
		}

		private void moreButton_Click(object sender, System.EventArgs e)
		{
			lessButton.Visible = true;
			moreButton.Visible = false;
			detailsPanel.Visible = true;
			this.FormBorderStyle = FormBorderStyle.Sizable;
			this.Height = _detailedFormHeight;
		}

		private void lessButton_Click(object sender, System.EventArgs e)
		{
			moreButton.Visible = true;
			lessButton.Visible = false;
			detailsPanel.Visible = false;
			this.FormBorderStyle = FormBorderStyle.FixedDialog;
			this.Height = _briefFormHeight;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ExceptionForm));
			this.buttonsPanel = new System.Windows.Forms.Panel();
			this.lessButton = new System.Windows.Forms.Button();
			this.okButton = new System.Windows.Forms.Button();
			this.moreButton = new System.Windows.Forms.Button();
			this.errorLabel = new System.Windows.Forms.Label();
			this.detailsTextBox = new System.Windows.Forms.TextBox();
			this.detailsPanel = new System.Windows.Forms.Panel();
			this.buttonsPanel.SuspendLayout();
			this.detailsPanel.SuspendLayout();
			this.SuspendLayout();
			// 
			// buttonsPanel
			// 
			this.buttonsPanel.AccessibleDescription = resources.GetString("buttonsPanel.AccessibleDescription");
			this.buttonsPanel.AccessibleName = resources.GetString("buttonsPanel.AccessibleName");
			this.buttonsPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("buttonsPanel.Anchor")));
			this.buttonsPanel.AutoScroll = ((bool)(resources.GetObject("buttonsPanel.AutoScroll")));
			this.buttonsPanel.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("buttonsPanel.AutoScrollMargin")));
			this.buttonsPanel.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("buttonsPanel.AutoScrollMinSize")));
			this.buttonsPanel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonsPanel.BackgroundImage")));
			this.buttonsPanel.Controls.Add(this.lessButton);
			this.buttonsPanel.Controls.Add(this.okButton);
			this.buttonsPanel.Controls.Add(this.moreButton);
			this.buttonsPanel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("buttonsPanel.Dock")));
			this.buttonsPanel.Enabled = ((bool)(resources.GetObject("buttonsPanel.Enabled")));
			this.buttonsPanel.Font = ((System.Drawing.Font)(resources.GetObject("buttonsPanel.Font")));
			this.buttonsPanel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("buttonsPanel.ImeMode")));
			this.buttonsPanel.Location = ((System.Drawing.Point)(resources.GetObject("buttonsPanel.Location")));
			this.buttonsPanel.Name = "buttonsPanel";
			this.buttonsPanel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("buttonsPanel.RightToLeft")));
			this.buttonsPanel.Size = ((System.Drawing.Size)(resources.GetObject("buttonsPanel.Size")));
			this.buttonsPanel.TabIndex = ((int)(resources.GetObject("buttonsPanel.TabIndex")));
			this.buttonsPanel.Text = resources.GetString("buttonsPanel.Text");
			this.buttonsPanel.Visible = ((bool)(resources.GetObject("buttonsPanel.Visible")));
			// 
			// lessButton
			// 
			this.lessButton.AccessibleDescription = resources.GetString("lessButton.AccessibleDescription");
			this.lessButton.AccessibleName = resources.GetString("lessButton.AccessibleName");
			this.lessButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lessButton.Anchor")));
			this.lessButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("lessButton.BackgroundImage")));
			this.lessButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lessButton.Dock")));
			this.lessButton.Enabled = ((bool)(resources.GetObject("lessButton.Enabled")));
			this.lessButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("lessButton.FlatStyle")));
			this.lessButton.Font = ((System.Drawing.Font)(resources.GetObject("lessButton.Font")));
			this.lessButton.Image = ((System.Drawing.Image)(resources.GetObject("lessButton.Image")));
			this.lessButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lessButton.ImageAlign")));
			this.lessButton.ImageIndex = ((int)(resources.GetObject("lessButton.ImageIndex")));
			this.lessButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lessButton.ImeMode")));
			this.lessButton.Location = ((System.Drawing.Point)(resources.GetObject("lessButton.Location")));
			this.lessButton.Name = "lessButton";
			this.lessButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lessButton.RightToLeft")));
			this.lessButton.Size = ((System.Drawing.Size)(resources.GetObject("lessButton.Size")));
			this.lessButton.TabIndex = ((int)(resources.GetObject("lessButton.TabIndex")));
			this.lessButton.Text = resources.GetString("lessButton.Text");
			this.lessButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lessButton.TextAlign")));
			this.lessButton.Visible = ((bool)(resources.GetObject("lessButton.Visible")));
			this.lessButton.Click += new System.EventHandler(this.lessButton_Click);
			// 
			// okButton
			// 
			this.okButton.AccessibleDescription = resources.GetString("okButton.AccessibleDescription");
			this.okButton.AccessibleName = resources.GetString("okButton.AccessibleName");
			this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("okButton.Anchor")));
			this.okButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("okButton.BackgroundImage")));
			this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.okButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("okButton.Dock")));
			this.okButton.Enabled = ((bool)(resources.GetObject("okButton.Enabled")));
			this.okButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("okButton.FlatStyle")));
			this.okButton.Font = ((System.Drawing.Font)(resources.GetObject("okButton.Font")));
			this.okButton.Image = ((System.Drawing.Image)(resources.GetObject("okButton.Image")));
			this.okButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("okButton.ImageAlign")));
			this.okButton.ImageIndex = ((int)(resources.GetObject("okButton.ImageIndex")));
			this.okButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("okButton.ImeMode")));
			this.okButton.Location = ((System.Drawing.Point)(resources.GetObject("okButton.Location")));
			this.okButton.Name = "okButton";
			this.okButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("okButton.RightToLeft")));
			this.okButton.Size = ((System.Drawing.Size)(resources.GetObject("okButton.Size")));
			this.okButton.TabIndex = ((int)(resources.GetObject("okButton.TabIndex")));
			this.okButton.Text = resources.GetString("okButton.Text");
			this.okButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("okButton.TextAlign")));
			this.okButton.Visible = ((bool)(resources.GetObject("okButton.Visible")));
			this.okButton.Click += new System.EventHandler(this.okButton_Click);
			// 
			// moreButton
			// 
			this.moreButton.AccessibleDescription = resources.GetString("moreButton.AccessibleDescription");
			this.moreButton.AccessibleName = resources.GetString("moreButton.AccessibleName");
			this.moreButton.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("moreButton.Anchor")));
			this.moreButton.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moreButton.BackgroundImage")));
			this.moreButton.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("moreButton.Dock")));
			this.moreButton.Enabled = ((bool)(resources.GetObject("moreButton.Enabled")));
			this.moreButton.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("moreButton.FlatStyle")));
			this.moreButton.Font = ((System.Drawing.Font)(resources.GetObject("moreButton.Font")));
			this.moreButton.Image = ((System.Drawing.Image)(resources.GetObject("moreButton.Image")));
			this.moreButton.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moreButton.ImageAlign")));
			this.moreButton.ImageIndex = ((int)(resources.GetObject("moreButton.ImageIndex")));
			this.moreButton.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("moreButton.ImeMode")));
			this.moreButton.Location = ((System.Drawing.Point)(resources.GetObject("moreButton.Location")));
			this.moreButton.Name = "moreButton";
			this.moreButton.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("moreButton.RightToLeft")));
			this.moreButton.Size = ((System.Drawing.Size)(resources.GetObject("moreButton.Size")));
			this.moreButton.TabIndex = ((int)(resources.GetObject("moreButton.TabIndex")));
			this.moreButton.Text = resources.GetString("moreButton.Text");
			this.moreButton.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("moreButton.TextAlign")));
			this.moreButton.Visible = ((bool)(resources.GetObject("moreButton.Visible")));
			this.moreButton.Click += new System.EventHandler(this.moreButton_Click);
			// 
			// errorLabel
			// 
			this.errorLabel.AccessibleDescription = resources.GetString("errorLabel.AccessibleDescription");
			this.errorLabel.AccessibleName = resources.GetString("errorLabel.AccessibleName");
			this.errorLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("errorLabel.Anchor")));
			this.errorLabel.AutoSize = ((bool)(resources.GetObject("errorLabel.AutoSize")));
			this.errorLabel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("errorLabel.Dock")));
			this.errorLabel.Enabled = ((bool)(resources.GetObject("errorLabel.Enabled")));
			this.errorLabel.Font = ((System.Drawing.Font)(resources.GetObject("errorLabel.Font")));
			this.errorLabel.Image = ((System.Drawing.Image)(resources.GetObject("errorLabel.Image")));
			this.errorLabel.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("errorLabel.ImageAlign")));
			this.errorLabel.ImageIndex = ((int)(resources.GetObject("errorLabel.ImageIndex")));
			this.errorLabel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("errorLabel.ImeMode")));
			this.errorLabel.Location = ((System.Drawing.Point)(resources.GetObject("errorLabel.Location")));
			this.errorLabel.Name = "errorLabel";
			this.errorLabel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("errorLabel.RightToLeft")));
			this.errorLabel.Size = ((System.Drawing.Size)(resources.GetObject("errorLabel.Size")));
			this.errorLabel.TabIndex = ((int)(resources.GetObject("errorLabel.TabIndex")));
			this.errorLabel.Text = resources.GetString("errorLabel.Text");
			this.errorLabel.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("errorLabel.TextAlign")));
			this.errorLabel.Visible = ((bool)(resources.GetObject("errorLabel.Visible")));
			this.errorLabel.Click += new System.EventHandler(this.errorLabel_Click);
			// 
			// detailsTextBox
			// 
			this.detailsTextBox.AccessibleDescription = resources.GetString("detailsTextBox.AccessibleDescription");
			this.detailsTextBox.AccessibleName = resources.GetString("detailsTextBox.AccessibleName");
			this.detailsTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("detailsTextBox.Anchor")));
			this.detailsTextBox.AutoSize = ((bool)(resources.GetObject("detailsTextBox.AutoSize")));
			this.detailsTextBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("detailsTextBox.BackgroundImage")));
			this.detailsTextBox.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("detailsTextBox.Dock")));
			this.detailsTextBox.Enabled = ((bool)(resources.GetObject("detailsTextBox.Enabled")));
			this.detailsTextBox.Font = ((System.Drawing.Font)(resources.GetObject("detailsTextBox.Font")));
			this.detailsTextBox.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("detailsTextBox.ImeMode")));
			this.detailsTextBox.Location = ((System.Drawing.Point)(resources.GetObject("detailsTextBox.Location")));
			this.detailsTextBox.MaxLength = ((int)(resources.GetObject("detailsTextBox.MaxLength")));
			this.detailsTextBox.Multiline = ((bool)(resources.GetObject("detailsTextBox.Multiline")));
			this.detailsTextBox.Name = "detailsTextBox";
			this.detailsTextBox.PasswordChar = ((char)(resources.GetObject("detailsTextBox.PasswordChar")));
			this.detailsTextBox.ReadOnly = true;
			this.detailsTextBox.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("detailsTextBox.RightToLeft")));
			this.detailsTextBox.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("detailsTextBox.ScrollBars")));
			this.detailsTextBox.Size = ((System.Drawing.Size)(resources.GetObject("detailsTextBox.Size")));
			this.detailsTextBox.TabIndex = ((int)(resources.GetObject("detailsTextBox.TabIndex")));
			this.detailsTextBox.Text = resources.GetString("detailsTextBox.Text");
			this.detailsTextBox.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("detailsTextBox.TextAlign")));
			this.detailsTextBox.Visible = ((bool)(resources.GetObject("detailsTextBox.Visible")));
			this.detailsTextBox.WordWrap = ((bool)(resources.GetObject("detailsTextBox.WordWrap")));
			// 
			// detailsPanel
			// 
			this.detailsPanel.AccessibleDescription = resources.GetString("detailsPanel.AccessibleDescription");
			this.detailsPanel.AccessibleName = resources.GetString("detailsPanel.AccessibleName");
			this.detailsPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("detailsPanel.Anchor")));
			this.detailsPanel.AutoScroll = ((bool)(resources.GetObject("detailsPanel.AutoScroll")));
			this.detailsPanel.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("detailsPanel.AutoScrollMargin")));
			this.detailsPanel.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("detailsPanel.AutoScrollMinSize")));
			this.detailsPanel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("detailsPanel.BackgroundImage")));
			this.detailsPanel.Controls.Add(this.detailsTextBox);
			this.detailsPanel.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("detailsPanel.Dock")));
			this.detailsPanel.Enabled = ((bool)(resources.GetObject("detailsPanel.Enabled")));
			this.detailsPanel.Font = ((System.Drawing.Font)(resources.GetObject("detailsPanel.Font")));
			this.detailsPanel.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("detailsPanel.ImeMode")));
			this.detailsPanel.Location = ((System.Drawing.Point)(resources.GetObject("detailsPanel.Location")));
			this.detailsPanel.Name = "detailsPanel";
			this.detailsPanel.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("detailsPanel.RightToLeft")));
			this.detailsPanel.Size = ((System.Drawing.Size)(resources.GetObject("detailsPanel.Size")));
			this.detailsPanel.TabIndex = ((int)(resources.GetObject("detailsPanel.TabIndex")));
			this.detailsPanel.Text = resources.GetString("detailsPanel.Text");
			this.detailsPanel.Visible = ((bool)(resources.GetObject("detailsPanel.Visible")));
			// 
			// ExceptionForm
			// 
			this.AcceptButton = this.okButton;
			this.AccessibleDescription = resources.GetString("$this.AccessibleDescription");
			this.AccessibleName = resources.GetString("$this.AccessibleName");
			this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize")));
			this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll")));
			this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin")));
			this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize")));
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.ControlBox = false;
			this.Controls.Add(this.detailsPanel);
			this.Controls.Add(this.errorLabel);
			this.Controls.Add(this.buttonsPanel);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.MaximizeBox = false;
			this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize")));
			this.MinimizeBox = false;
			this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize")));
			this.Name = "ExceptionForm";
			this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft")));
			this.ShowInTaskbar = false;
			this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition")));
			this.Text = resources.GetString("$this.Text");
			this.buttonsPanel.ResumeLayout(false);
			this.detailsPanel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

	}
}
